package com.tudulist.web;

import static com.tudulist.web.utils.TudulistUtil.TODOLISTID;

import java.util.Date;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitLink;
import org.apache.wicket.extensions.yui.calendar.DateTimeField;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.joda.time.DateTime;

import com.tudulist.bo.TodoManager;
import com.tudulist.model.Todo;
import com.tudulist.web.utils.TudulistUtil;

@SuppressWarnings("serial")
public class TodoPage extends WebPage {

	@SpringBean
	TodoManager todoManager;

	private Long todolistId;

	private WebMarkupContainer latestTodoContainer;

	private FeedbackPanel feedBackpanel;

	@SuppressWarnings("unused")
	private List<Todo> latestTodo;

	@Override
	protected void onInitialize() {
		super.onInitialize();
		PageParameters pageParam = getPageParameters();
		if (pageParam != null) {
			todolistId = pageParam.get(TODOLISTID).toLongObject();
		}

		buildCreateTodo();
		buildLatestTodo();
		buildFeedBack();
	}

	private void buildCreateTodo() {
		final TextField<String> title = new TextField<String>("title", new Model<String>(""));
		final TextArea<String> description = new TextArea<String>("description", new Model<String>(""));

		final DateTimeField dueDate = new DateTimeField("dueDate", new Model<Date>()) {
			@Override
			protected boolean use12HourFormat() {
				return false;
			}
		};

		Form<Void> form = new Form<Void>("form");
		form.add(title);
		form.add(description);
		form.add(dueDate);
		form.add(new AjaxSubmitLink("submit") {
			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> form) {
				Todo todo = new Todo();
				todo.setCreationDate(new DateTime());
				todo.setTitle(title.getModelObject());
				todo.setDescription(description.getModelObject());
				todo.setDueDate(new DateTime(dueDate.getModelObject()));
				todoManager.createTodo(todo, todolistId);
				info("Todo created with success.");
				target.add(feedBackpanel);
				target.add(latestTodoContainer);
			}
		});
		add(form);
	}

	/**
	 * build latest todo section
	 */
	private void buildLatestTodo() {
		latestTodoContainer = new WebMarkupContainer("latestTodoContainer");

		ListView<Todo> latestTodoView = new ListView<Todo>("latestTodoView", new PropertyModel<List<? extends Todo>>(this, "latestTodo")) {
			@Override
			protected void populateItem(final ListItem<Todo> item) {
				AjaxLink<Todo> todoEditLink = new AjaxLink<Todo>("todoEditLink", item.getModel()) {

					@Override
					public void onClick(AjaxRequestTarget target) {
						// TODO
					}

				};

				AjaxLink<Todo> todoRemoveLink = new AjaxLink<Todo>("todoRemoveLink", item.getModel()) {
					@Override
					public void onClick(AjaxRequestTarget target) {
						todoManager.remove(item.getModel().getObject().getId());
						info("Todo removed with success.");
						target.add(feedBackpanel);
						target.add(latestTodoContainer);
					}

				};
				item.add(new Label("todoTitleLabel", item.getModel().getObject().getTitle()));
				item.add(new Label("todoDescLabel", item.getModel().getObject().getDescription()));
				item.add(new Label("todoDueDateLabel", TudulistUtil.doFormat(item.getModel().getObject().getDueDate())));
				item.add(todoRemoveLink);
				item.add(todoEditLink);

			}

		};
		latestTodoView.setOutputMarkupId(true);
		latestTodoContainer.setOutputMarkupId(true);
		latestTodoContainer.add(latestTodoView);
		add(latestTodoContainer);

	}

	public List<Todo> getLatestTodo() {
		return todoManager.findAll(todolistId);
	}

	/**
	 * form feedback panel
	 */
	private void buildFeedBack() {
		feedBackpanel = new FeedbackPanel("feedback");
		feedBackpanel.setOutputMarkupId(true);
		add(feedBackpanel);

	}

}
